package com.dhsc.service.impl.ywzj.kbpzgl.pzglbb;

import com.dhsc.domain.ywzx.kbpzgl.DlpzsyqkVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.ywzj.kbpzgl.pzglbb.IDlpzsyqkDao;
import com.dhsc.service.ywzj.pzglbb.IDlpzsyqkService;
import com.dhsc.util.CommonUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author HP
 * @date 2024/5/27 15:32
 */
@Service
@Slf4j
public class DlpzsyqkServiceImpl implements IDlpzsyqkService {

    @Autowired
    private IDlpzsyqkDao dlpzsyqkDao;
    
	private static final String ZBDM = "99999";

    @Override
    public LayuiTableVo queryDlpzsyqkinfo(DlpzsyqkVo d) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            if(!CommonUtil.isNotEmptyObj(d.getState())){
                PageMethod.startPage(d.getPage(), d.getLimit());
            }
            List<DlpzsyqkVo> list = dlpzsyqkDao.queryDlpzsyqkinfo(d);
            PageInfo<DlpzsyqkVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
            obj.setMsg("");
        }catch (Exception e){
            log.error("查询单类凭证管理使用情况出错",e);
            throw new MyException("查询单类凭证管理使用情况出错",e);
        }
        return obj;

    }

    @Override
    public ResponseObject queryPzmc(DlpzsyqkVo d) {
        ResponseObject obj = new ResponseObject();
        try {
            List<DlpzsyqkVo> list = dlpzsyqkDao.queryPzmc(d);
            obj.setData(list);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("查询凭证名称出错",e);
            throw new MyException("查询凭证名称出错",e);
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryDlpzBdxx(DlpzsyqkVo d) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(d.getPage(), d.getLimit());
            List<DlpzsyqkVo> list = dlpzsyqkDao.queryDlpzBdxx(d);
            list.stream().forEach(i->{
                if(!i.getLslx().equals("00") && !i.getLslx().equals("01")){
                    i.setRksl(-i.getRksl());
                }
            });
            PageInfo<DlpzsyqkVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
            obj.setMsg("");
        }catch (Exception e){
            log.error("查询单类凭证变动信息出错",e);
            throw new MyException("查询单类凭证变动信息出错",e);
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryPzzhsyqk(DlpzsyqkVo d) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            if(!CommonUtil.isNotEmptyObj(d.getState())){
                PageMethod.startPage(d.getPage(), d.getLimit());
            }
            List<DlpzsyqkVo> list = null;
            if(CommonUtil.isNotEmptyObj(d.getYyb()) && !d.getYyb().equals(ZBDM)){
                list = dlpzsyqkDao.queryFbPzzhsyqk(d);
            }else{
                list = dlpzsyqkDao.queryQbPzzhsyqk(d);
            }
            PageInfo<DlpzsyqkVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
            obj.setMsg("");
        }catch (Exception e){
            log.error("查询凭证综合使用情况出错",e);
            throw new MyException("查询凭证综合使用情况出错",e);
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryRkLs(DlpzsyqkVo d) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(d.getPage(), d.getLimit());
            List<DlpzsyqkVo> list = null;
            if(!d.getYyb().equals(ZBDM)){
                list = dlpzsyqkDao.queryFbRklsxx(d);
            }else{
                list = dlpzsyqkDao.queryZbRklsxx(d);
            }
            PageInfo<DlpzsyqkVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
            obj.setMsg("");
        }catch (Exception e){
            log.error("查询入库流水出错",e);
            throw new MyException("查询入库流水出错",e);
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryYlzfls(DlpzsyqkVo d) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(d.getPage(), d.getLimit());
            List<DlpzsyqkVo> list = null;
            if(!d.getYyb().equals(ZBDM)){
                list = dlpzsyqkDao.queryFbYlzfls(d);
            }else{
                list = dlpzsyqkDao.queryZbYlzfls(d);
            }
            PageInfo<DlpzsyqkVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
            obj.setMsg("");
        }catch (Exception e){
            log.error("查询已领作废流水信息出错",e);
            throw new MyException("查询已领作废流水信息出错",e);
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryCklyls(DlpzsyqkVo d) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(d.getPage(), d.getLimit());
            List<DlpzsyqkVo> list = null;
            if(!d.getYyb().equals(ZBDM)){
                list = dlpzsyqkDao.queryFbCklyls(d);
            }else{
                list = dlpzsyqkDao.queryZbCklyls(d);
            }
            PageInfo<DlpzsyqkVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
            obj.setMsg("");
        }catch (Exception e){
            log.error("查询出库领用流水信息出错",e);
            throw new MyException("查询出库领用流水信息出错",e);
        }
        return obj;
    }
}
